var OvertimeManaged = function() {
	return {
		datatable : null,
		init : function() {
			this.initSearch();
			this.initListener();
		},
		initSearch : function() {
			if (!jQuery().dataTable) {
				return;
			}
			var pictureTable = $("#overtimeListTable").dataTable({
				"sAjaxSource" : ctx + "/overtime/search?now=" + new Date().getTime(),
				/* 使用post方式 */
				"fnServerData" : function(sSource, aoData, fnCallback) {
					var postData = {
						page : 1,
						start : 0,
						rows : 10,
						params : {}
					};
					// 参数
					for (var i = 0, size = aoData.length; i < size; i++) {
						if (aoData[i].name == 'iDisplayLength') {
							postData.rows = aoData[i].value;
						} else if (aoData[i].name == 'iDisplayStart') {
							postData.start = aoData[i].value;
						} else if (aoData[i].name == 'sEcho') {
							postData.page = aoData[i].value;
						}
					}

					$.ajax({
						"dataType" : 'json',
						"type" : "POST",
						"cache": false,
						"url" : sSource,
						"data" : postData,
						"success" : function(data, textStatus, jqXHR){
							fnCallback(data.data, textStatus, jqXHR);
						}
					});
				},
				"bAutoWidth":false,
				"aoColumnDefs" : [
						
						{
							"data" : "index",
							"aTargets" : [ 0 ]
						},{
							"data" : "onDutyTime",
							"aTargets" : [ 1 ],
							"mRender" : function(data, type, full) {
								return getStringDate(full.onDutyTime);
							}
						},
						{
							"data" : "offDutyTime",
							"aTargets" : [ 2 ],
							"mRender" : function(data, type, full) {
								return getStringDate(full.offDutyTime);
							}
						},
						{
							"data" : "isHolidays",
							"aTargets" : [ 3 ],
							"mRender" : function(data, type, full) {
								if(full.isHolidays){
									return "是";
								}else{
									return "不是";
								}
							}
						},
						{
							"data" : "note",
							"aTargets" : [ 4 ]
						},{
							"data" : "status",
							"aTargets" : [ 5 ],
							"mRender" : function(data, type, full) {
								var statusName="";
								if(full.status == 10){
									statusName="待提交";
								} else if(full.status == 20){
									statusName="待审核";
								} else if(full.status == 30){
									statusName="审核中";
								} else if(full.status == 40){
									statusName="已同意";
								} else if(full.status == 50){
									statusName="已驳回";
								} 
								return statusName;
							}
						},
						{
							"aTargets" : [ 6 ],
							"mRender" : function(data, type, full) {
								var btns =  '';
								if(full.status == 10){
									btns += ' <button title="'+messages.public_button_edit+'" class="btn btn-sm-table btn-primary" name="submit" target_id="'+full.id+'">提交申请</button>';
									btns += ' <button title="'+messages.public_button_edit+'" class="btn btn-sm-table btn-primary" name="edit" target_id="'+full.id+'">编辑</button>';
									btns += ' <button title="'+messages.public_button_del+'" class="btn btn-sm-table  btn-danger" name="del" target_id="'+full.id+'">删除</button>';
									/*btns +=  Permission.createPermElement('group:list:modify',' <button title="'+messages.public_button_edit+'" class="btn btn-sm-table btn-primary" name="edit" target_id="'+full.id+'">编辑</button>');
									btns +=  Permission.createPermElement('group:list:del',' <button title="'+messages.public_button_del+'" class="btn btn-sm-table  btn-danger" name="del" target_id="'+full.id+'">删除</button>');	*/	
								} else if(full.status == 20){
								} else if(full.status == 30){
								} else if(full.status == 40){
								} else if(full.status == 50){
								} 
								return btns;
							}
						} ]
			});
			this.datatable = pictureTable;
		},
		reSearch:function(){
        	this.datatable.fnDraw();
        },removeClass:function(){//去除Form表单的验证样式
        	$('.form-group','#actForm').removeClass('has-success')
            .removeClass('has-error');
    		 $('span').remove('.help-block');
        },initListener : function() {
        	
        	/* 时间控制绑定 */
        	$("#onDutyTime").datetimepicker({lang:'ch',timepicker:false, format:"Y-m-d H:i:s"});
			$("#offDutyTime").datetimepicker({lang:'ch',timepicker:false, format:"Y-m-d H:i:s"});
			
			/*
			 * 添加
			 */
			$("#addBtn").click(function() {
				OvertimeManaged.removeClass();
				$("div[name=editDlg] h4.modal-title").html("新增加班申请单");
				$("#actForm")[0].reset();
				$("#actForm").find("input[name='id']").val("");
				$("#editDlg").modal("show");
			});
			
			$("#actFormBtn").click(function(){
				var form1 = $('#actForm');
				form1.validate({
					rules : {
						onDutyTime : {
							required : true
						},
						offDutyTime : {
							required : true
						}
					}
				});
				var errorValidate = function(){
					var onDutyTime = form1.find('#onDutyTime').val(),
						offDutyTime = form1.find('#offDutyTime').val(),
						sTime = (new Date(Date.parse(onDutyTime.replace(/-/g, "/")))).getTime(),
						eTime = (new Date(Date.parse(offDutyTime.replace(/-/g, "/")))).getTime(),
						error = 0,
						errorMsg = "";
					if(sTime >= eTime){
						error = 1;
						errorMsg = "加班开始时间不能大于结束时间";
					}
					
					if(error){
						BootstrapDialog.show({
							type : BootstrapDialog.TYPE_WARNING,
							title : messages.public_cue_warning,
							message : errorMsg,
							buttons : [ {
								label : messages.public_cue_sure,
								cssClass : 'btn btn-sm btn-primary',
								action : function(dialogItself) {
									dialogItself.close();
								}
							} ]
						});
						return false;
					}else{
						return true;
					}
				}
				if(form1.valid() && errorValidate()){
					var postData = $("form[name='actForm']").serializeJson();
					$.ajax({
						"dataType" : 'json',
						"cache": false,
						"type" : "POST",
						"data" : postData,
						"url" : ctx + "/overtime/edit?now="+new Date().getTime(),
						"success" : function(data) {
							if(data.type=='success'){
								$("#editDlg").modal("hide");
								BootstrapDialog.show({
									title : messages.public_cue_cue,
									message : messages.public_cue_saveOk,
									buttons : [ {
										label : messages.public_cue_sure,
										cssClass : 'btn btn-sm btn-primary',
										action : function(dialogItself) {
											OvertimeManaged.reSearch();
											dialogItself.close();
										}
									} ]
								});
								
							}else{
								BootstrapDialog.show({
									type : BootstrapDialog.TYPE_WARNING,
									title : messages.public_cue_warning,
									message : data.message,
									buttons : [ {
										label : messages.public_cue_sure,
										cssClass : 'btn btn-sm btn-primary',
										action : function(dialogItself) {
											OvertimeManaged.reSearch();
											dialogItself.close();
										}
									} ]
								});
							}
						}
					});
				}
			});
			
			/*
			 * 提交申请
			 * */
			$('#overtimeListTable tbody').on('click', 'button[name="submit"]', function(){
				var id = $(this).attr("target_id");
				PersonnelManager.init(function(users){
					OvertimeManaged.submitApply(id, users);
				});
			});
			
			/*
			 * 编辑
			 */
			$('#overtimeListTable tbody')
				.on('click', 'button[name="edit"]',function() {
					var id = $(this).attr("target_id");
					var form=$("form[name='actForm']");
					OvertimeManaged.removeClass();
					$.ajax({
						"dataType" : 'json',
						"cache" : false,
						"type" : "POST",
						"data" : {},
						"url" : ctx + '/overtime/get/'+id+"?now="+new Date().getTime(),
						"success" : function(data, textStatus, jqXHR){
							if (data.type == 'success') {
								$(form).binddata(data.data);
								$("div[name=editDlg] h4.modal-title").html("编辑加班申请单");
								var sTime = Number($(form).find('#onDutyTime').val()),
									eTime = Number($(form).find('#offDutyTime').val());
								$(form).find('#onDutyTime').val(getStringDate(sTime));
								$(form).find('#offDutyTime').val(getStringDate(eTime));
								$("#editDlg").modal("show");
							} else {
								BootstrapDialog.show({
									title : messages.public_cue_message,
									message : data.message,
									buttons : [ {
										label : messages.public_cue_sure,
										action : function(dialog) {
											dialog.close();
										}
									} ]
								});
							}
						}
					});
				});
			$('#overtimeListTable tbody')
				.on('click', 'button[name="del"]',function() {
					var id = $(this).attr("target_id");
					OvertimeManaged.del(id);
				});
		},
		/**
		 * 删除
		 */
		del : function(id) {
			BootstrapDialog.show({
				type : BootstrapDialog.TYPE_WARNING,
				title : messages.public_cue_warning,
				message : messages.public_cue_delSure,
				buttons : [ {
					label : messages.public_cue_close,
					cssClass: 'btn btn-sm btn-default',
					action : function(dialogItself) {
						dialogItself.close();
					}
				}, {
					label : messages.public_cue_sure,
					cssClass : 'btn btn-sm btn-primary',
					action : function(dialogItself) {
						OvertimeManaged.sureDel(id);
						dialogItself.close();
					}
				} ]
			});
		},
		sureDel : function(id){
			$.ajax({
				"dataType" : 'json',
				"cache": false,
				"type" : "POST",
				"data" : {},
				"url" : ctx + '/overtime/delete/'+id+"?now="+new Date().getTime(),
				"success" : function(data, textStatus, jqXHR){
					if (data.type == 'success') {
						BootstrapDialog.show({
							title : messages.public_cue_message,
							message : messages.public_cue_delSuccess,
							buttons : [ {
								label : messages.public_cue_sure,
								action : function(dialog) {
									OvertimeManaged.reSearch();
									dialog.close();
								}
							} ]
						});
					} else {
						BootstrapDialog.show({
							title : messages.public_cue_message,
							message : data.message,
							buttons : [ {
								label : messages.public_cue_sure,
								action : function(dialog) {
									dialog.close();
								}
							} ]
						});
					}
				}
			});
		},
		submitApply : function(id, users){
			$.ajax({
				"dataType" : 'json',
				"cache" : false,
				"type" : "POST",
				"data" : {leaderships:users},
				"url" : ctx + '/overtime/apply/'+id+"?now="+new Date().getTime(),
				"success" : function(data, textStatus, jqXHR){
					if (data.type == 'success') {
						BootstrapDialog.show({
							title : messages.public_cue_cue,
							message : "提交成功",
							buttons : [ {
								label : messages.public_cue_sure,
								cssClass : 'btn btn-sm btn-primary',
								action : function(dialogItself) {
									OvertimeManaged.reSearch();
									dialogItself.close();
								}
							} ]
						});
					} else {
						BootstrapDialog.show({
							title : messages.public_cue_message,
							message : data.message,
							buttons : [ {
								label : messages.public_cue_sure,
								action : function(dialog) {
									dialog.close();
								}
							} ]
						});
					}
				}
			});
		}
	};
}();